<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // ----- 代码 1 async 函数返回一个 Promise 对象。-----
        async function asyncFunc() {
            return 'asyncFunc';
        }
        // asyncFunc 函数等价于 normalFunc 函数的写法
        function normalFunc(){
            return new Promise((resolve,reject)=>{
                resolve('normalFunc');
            })
        }
        const asyncResult = asyncFunc();
        const funcResult = normalFunc();
        console.log(asyncResult); // Promise { 'asyncFunc' }
        console.log(funcResult); // Promise { 'normalFunc' }
        // ----- 代码 2 await 命令正常情况下后面是一个 Promise 对象-----
        async function awaitFunc1() {
            // 报错：SyntaxError: await is only valid in async functions...
            return await new Promise((resolve,reject)=>{
                resolve('awaitFunc1');
            })
        }
        // 调用 awaitFunc 函数获得 Promise 对象，用 then 方法获取结果
        awaitFunc1().then(res => console.log(res)); // 'awaitFunc1'
        // ----- 代码 3 多个 await 命令后面的异步操作-----
        function sleep(time, value) {
            return new Promise((resolve,reject)=>{
                setTimeout(()=>resolve(value),time);
            })
        }
        async function awaitFunc2() {
            let start = Date.now();
            let a1 = await sleep(1000, 'a1');
            let a2 = await sleep(2000, 'a2');
            let end = Date.now();
            console.log(`awaitFunc2 result = ${[a1, a2]} , 耗时：${end - start}ms`);
        }
        async function awaitFunc3() {
            let start = Date.now();
            let result = await Promise.all([sleep(1000, 'a1'), sleep(2000, 'a2')]);
            let end = Date.now();
            console.log(`awaitFunc3 result = ${result} , 耗时：${end - start}ms`);
        }
        awaitFunc2();// 耗时：3000+ms
        awaitFunc3(); // 耗时：2000+ms
         // ----- 代码 4 await 命令后面的 Promise 对象如果变为 reject 状态-----
        async function awaitFunc4() {
            let result1 = await new Promise((resolve,reject)=>{
                console.log("---------执行 reject1----------")
                reject('reject1');
            });
            let result2 = await new Promise((resolve,reject)=>{
                console.log("---------执行 resolve2----------")
                resolve ('resolve2');
            });
            console.log("-------------全部执行完毕--------------")
            return result1 + "-" + result2;
        }
        awaitFunc4()
        .then(res => console.log("then", res))
        .catch(err => console.log("catch", err)); // catch reject1
        let result1;
        try {
            result1 = await new Promise((resolve, reject) => {
            console.log("---------执行 reject1----------")
            reject('reject1');
            });
        } catch (err) {
            console.log("catch", err);
        }
        // ----- 代码 5 await 只能出现在 async 函数中，如果在普通函数中，报错-----
        function awaitFunc5() {
            // 报错：SyntaxError: await is only valid in async functions...
            await new Promise((resolve, reject) => {
                resolve('awaitFunc5');
            })
        }
        awaitFunc5();
    </script>
</head>
<body>
    
</body>
</html>